<?php

namespace app\backstage\model;

use think\Model;
use think\Validate;
use Db;
class Auth extends Model
{   

    // protected $insert = ['create_time' => 5];  //自动插入创建时间  
    // protected $insert = ['create_time' => 3];  

    protected $name = 'rbac_auth';
    //权限排序
    public function sortData($data){

        // //分开顶级权限、下级权限
        

        $topAuth = array();
        $sonAuth = array();

        foreach($data as $v){
            if($v['fid']==0){
                $topAuth[$v['id']] = $v;
            }else{
                $sonAuth[$v['fid']][$v['id']] = $v;
            }
        }
      

        // //顶级权限插入到子级权限前面
        foreach($topAuth as $k=>$v){
   
          if(isset($sonAuth[$v['id']])){
          
            array_unshift($sonAuth[$v['id']],$v);
          }else{
            $sonAuth[$v['id']][]=$v;
          }
        }

        $dataList = array();

        foreach($sonAuth as $v){
            foreach($v as $vv){
                if($vv['pid']==null){
                    $vv['pid'] = '最高级';
                }else{
                    $vv['auth_name'] = ' 𠃊  '.$vv['auth_name'];
                }

                array_push($dataList,$vv);

            }
        }

        // foreach($data as $k =>&$v){
        //     $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
        //     if($v['pid']==null){
        //         $v['pid'] = '最高级';
        //     }else{
        //         $v['auth_name'] = '-- '.$v['auth_name'];
        //     }
        // }

        return $dataList;
    }


    //验证数据
    public function valiData($data){

        $validate = new Validate([
            'auth_name'  => 'require',
//            'auth_controller'  => 'require',
            'pid' => 'require',
//            '__token__' => 'require|token',
        ]);
            
        
        if (!$validate->check($data)) {
            return ['code'=>101,'msg'=>$validate->getError()];
        }
        
        return ['code'=>200];

    }


     //验证数据
    public function valiData2($data){
        
        $validate = new Validate([
            'module_name'  => 'require',
            'namespace' => 'require',
            '__token__' => 'require|token',
        ]);
            
        if (!$validate->check($data)) {
            return ['code'=>101,'msg'=>$validate->getError()];
        }
        
        return ['code'=>200];

    }

    //生成列表数据
    public function createList($data){
        
        //顶级权限
        $parentAuth = array(
            'pid' =>0,
            'auth_name' =>$data['module_name'].'管理',
            'create_time' =>time(),
            'auth_controller'=>$data['namespace']
        );

        $this->data($parentAuth);

        Db::startTrans();
        $this->save();
 
        if($pid=$this->id){
            $list = [
                ['pid'=>$pid,'auth_name'=>$data['module_name'].'列表展示','auth_controller'=>$data['namespace'],'auth_action'=>'index','create_time'=>time()],
                ['pid'=>$pid,'auth_name'=>$data['module_name'].'新增页','auth_controller'=>$data['namespace'],'auth_action'=>'create','create_time'=>time()],
                ['pid'=>$pid,'auth_name'=>$data['module_name'].'新增保存','auth_controller'=>$data['namespace'],'auth_action'=>'save','create_time'=>time()],
                ['pid'=>$pid,'auth_name'=>$data['module_name'].'编辑页','auth_controller'=>$data['namespace'],'auth_action'=>'edit','create_time'=>time()],
                ['pid'=>$pid,'auth_name'=>$data['module_name'].'编辑修改','auth_controller'=>$data['namespace'],'auth_action'=>'update','create_time'=>time()],
                ['pid'=>$pid,'auth_name'=>$data['module_name'].'删除','auth_controller'=>$data['namespace'],'auth_action'=>'delete','create_time'=>time()]
            ];
            
            $res = $this->insertAll($list);
            
            if($res){
                Db::commit();
                return true;
                
            }else{
                Db::rollback();
                return false;
            }

        }else{
            return false;
        }

        $auth->saveAll($list);
       
    }
}
